/*
 * @Description:
 * @Autor: luojie
 * @Date: 2021-07-03 08:59:38
 * @LastEditors: luojie
 * @LastEditTime: 2021-07-28 11:42:55
 */
import store from '../store'
export default (params) => {
  var baseUrl = process.env.VUE_APP_BASE_URL
  let data = params.data || {};

  const isGldWorker = data.isGldWorker;
  if (isGldWorker) {
    delete params.data.isGldWorker;
  }
  let token = uni.getStorageSync('token')
  // console.log(111, {...params});
  return new Promise((resolve, reject) => {
    uni.request({
      ...params,
      url: `${baseUrl}${params.url}`,
      header: params.header || {
        "content-type": "application/json;charset=UTF-8",
        'x-token': token || '1234567890'
      },
      success: (res) => {
        if (res.data.code === 200) {
          resolve(res.data.data)
        } else if (res.data.code === 401) {
          uni.removeStorage("token");
          uni.removeStorageSync('shenAiNin');
          let page = getCurrentPages().pop();
          uni.showToast({
            title: isGldWorker ? '您不是工作人员' : res.data.msg,
            duration: 3000,
            icon: 'none'
          });
          if (page.route != "pages/index/index") {
            uni.reLaunch({
              url: '/pages/index/index'
            })
          }
          store.commit('SET_AUTH_STATUS', false)
        } else {
          // 错误提示，未健康排查也可以进行综合评估
          if (res.data.msg != '未查询到相关信息，请先进行健康排查登记。') {
            uni.showToast({
              title: res.data.msg,
              duration: 3000,
              icon: 'none'
            });
          }
          reject(res.data)
        }
      },
      fail: (error) => {
        console.log(error)
        reject(error)
      }
    })
  })

}

// 附件下载
export function downloadFile(params) {
  var baseUrl = process.env.VUE_APP_BASE_URL
  let data = params.data || {};

  let token = uni.getStorageSync('token')
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${baseUrl}${params.url}`,
      data: params.data,
      method: 'POST',
      header: params.header || {
        "content-type": "application/json;charset=UTF-8",
        'x-token': token || '1234567890'
      },
      responseType: 'arraybuffer',
      success: (res) => {
        try {
          // 如果result不报错，说明后台返回的是json对象，则弹框提示，否则说明返回的是文件流，进入catch，下载文件
          // let enc = new TextDecoder('utf-8');
          // let result = JSON.parse(enc.decode(new Uint8Array(res.data))) //转化成json对象
          // if (result.msg) {
          //   reject(result.msg);
          //   uni.showToast({
          //     title: result.msg,
          //     icon: 'none'
          //   })
          // }
          // 替换TextDecoder方案
          let buffer = res.data;
          let dataview = new DataView(buffer);
          let ints = new Uint8Array(buffer.byteLength);
          let str = '';
          for (let i = 0; i < ints.length; i++) {
            str += String.fromCharCode(dataview.getUint8(i));
          }
          let result = JSON.parse(str);
          if (result.msg) {
            reject(result.msg);
          }
        } catch (err) {
          console.log(err)
          resolve(res.data)
        }
      },
      fail: (error) => {
        console.log(error)
        reject(error)
      }
    })
  })
}